Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-61808] Always transmit f:password values as Secret #4630

Merged
merged 2 commits into from May 10, 2020

Conversation

daniel-beck
Copy link
Member

See JENKINS-61808.

The <f:password/> form control has a magic behavior, provided by Functions#getPasswordValue and a converter in Secret.java:
If it's backed by a Secret, it will use the encrypted form of the password (or token, key, etc.) as value, rather than the plain text.
Additionally, if it's backed by a Secret, an Item is in the URL hierarchy, and the user lacks Item/Configure, it will only receive ****** to be rendered, instead of a real value.
The latter is less important since Jenkins 2.223, which introduced read-only forms for users with Item/ExtendedRead, and a representation of f:password that doesn't even attempt to render a real value.

Unfortunately, many plugin maintainers get this wrong and use String-based for values only shown using <f:password/>. This affects both plugins who get storage right (using Secret), and those that get that part wrong, too.

So what does this do?

  • When rendering an <f:password/>, even if it's not backed by a Secret, we will convert the value to be shown to a Secret and render its encrypted form in most cases.
    Additionally, we also allow non-Secret backed <f:password/> to be ****** out when the user lacks a relevant Item/Configure permission.
  • When processing form submissions, a new Converter will convert any value that's an encrypted Secret to its decrypted String representation.

In addition to the above, we also add a new warning to the log whenever a <f:password/> is backed by a string, and attempt to identify which view it's coming from (which is surprisingly annoying to do). This PR does not mean that plugins having String APIs wouldn't be a bug. This warning will appear when running jetty:run and hpi:run, but not in production use. It can also serve as a useful reminder that the underlying field should be a Secret.

Since the APIs for password parameters were using the String type, they have been adapted in this PR.

Escape hatches

This PR introduce more magic, so we have two different escape hatches in case things go sideways:

  • hudson.util.Secret.AUTO_ENCRYPT_PASSWORD_CONTROL is true by default and controls whether the basic new behavior is active. This must be set on startup, as we don't even want the converter to register if something goes wrong.
  • hudson.util.Secret.BLANK_NONSECRET_PASSWORD_FIELDS_WITHOUT_ITEM_CONFIGURE is true by default and controls whether the ***** masking that existed for Secret backed f:password fields is extended to those without a Secret.

Limitations

This introduces a slight regression with String-based "setter" APIs around a Secret field: A round-trip will now result in changes to the persisted value, as Jenkins transparently decrypts any submitted value. IOW, it now behaves as though the user always entered the same value manually.

In development mode, Jenkins will log warnings when it encounters secrets not backed by a Secret, including fields showing a fixed String value that's not protection-worthy. This should rarely occur legitimately. It would occur on "Build With Parameters" with password parameters, which use a fixed <DEFAULT> placeholder, if we didn't specifically support that in getPasswordValue, and it happened in the "Parameters" action of builds when not showing a password value, so I replaced the (inappropriate) use of f:password there.

If the storage of credentials is wrong, rather than just the Java API exposing values to form fields, then users can still obtain the plain-text stored credential using GET config.xml. But plain-text storage of credentials is a problem independent of the UI and remote API to begin with and would need to be addressed anyway.

This PR removes a protection that would make (some) unit tests fail:

if (getIsUnitTest() && !o.equals(PasswordParameterDefinition.DEFAULT_VALUE)) {
throw new SecurityException("attempted to render plaintext ‘" + o + "’ in password field; use a getter of type Secret instead");
}

We can probably restore that if desired, and set a flag to ignore that for these specific tests, but TBH I'm not sure how much this helps anyway.

Proposed changelog entries

  • Security hardening: Always round-trip password form control values in an encrypted form, even if not backed by an encrypted Secret field. In case of problems, this can be disabled by setting the system property hudson.util.Secret.AUTO_ENCRYPT_PASSWORD_CONTROL to false on startup.
  • Security hardening: Always use a placeholder value for password form control values in item related configuration forms when the user is missing Item/Configure permission, even if not backed by an encrypted Secret field. In case of problems, this can be disabled by setting the system property hudson.util.Secret.BLANK_NONSECRET_PASSWORD_FIELDS_WITHOUT_ITEM_CONFIGURE to false.

Or we can limit the details we expose to users here and just go with

  • Security hardening related to password form fields.

If there are real problems, we can provide the system properties in regression reports.

Proposed upgrade guidelines

N/A

Submitter checklist

  • JIRA issue is well described
  • Changelog entries and upgrade guidelines are appropriate for the audience affected by the change (users or developer, depending on the change). Examples
    • Fill-in the Proposed changelog entries section only if there are breaking changes or other changes which may require extra steps from users during the upgrade
  • Appropriate autotests or explanation to why this change has no tests
  • [n/a] For dependency updates: links to external changelogs and, if possible, full diffs

Desired reviewers

@mention

Maintainer checklist

Before the changes are marked as ready-for-merge:

  • There are at least 2 approvals for the pull request and no outstanding requests for change
  • Conversations in the pull request are over OR it is explicit that a reviewer does not block the change
  • Changelog entries in the PR title and/or Proposed changelog entries are correct
  • Proper changelog labels are set so that the changelog can be generated automatically
  • If the change needs additional upgrade steps from users, upgrade-guide-needed label is set and there is a Proposed upgrade guidelines section in the PR title. (example)
  • If it would make sense to backport the change to LTS, a JIRA issue should exist and be labeled as lts-candidate

@jglick
Copy link
Member

jglick commented Apr 7, 2020

This PR removes a protection that would make (some) unit tests fail

IIUC this means that a plugin which whose InjectedTest passed on old versions of Jenkins but failed with a useful error about String vs. Secret in current versions would now go back to passing despite the plugin not doing things the right way?

@daniel-beck
Copy link
Member Author

This PR removes a protection that would make (some) unit tests fail

IIUC this means that a plugin which whose InjectedTest passed on old versions of Jenkins but failed with a useful error about String vs. Secret in current versions would now go back to passing despite the plugin not doing things the right way?

Unsure what you mean. AFAICT, there is no InjectedTest for what this behavior, otherwise there wouldn't be so many plugins affected by this. I think we've discussed an idea for a generic test in the past, but it doesn't exist.

It requires a manually set up configuration that #configRoundtrip-ped and then a manual comparison of submitted values to notice that the password was essentially empty after submission. Nobody sees the SecurityException from Functions#getPasswordValue. If that exception was supposed to fail tests on being thrown, there may have been a regression along the way.

As I wrote, if we think there's value in having the test specific behavior, I can try to keep it around and just disable it for the newly added tests only; but I don't see a lot of value here.

Copy link
Member

@jvz jvz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -0,0 +1 @@
hidden=(password value not shown)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oleg-nenashev oleg-nenashev self-requested a review April 8, 2020 10:42
@oleg-nenashev oleg-nenashev added the rfe For changelog: Minor enhancement. use `major-rfe` for changes to be highlighted label Apr 10, 2020
@timja timja self-requested a review April 30, 2020 14:42
@timja
Copy link
Member

timja commented Apr 30, 2020

The code looks sensible, I would like to do some interactive testing before approving, but don't take that as a blocker if others approve this and are happy

/* Log a warning if we're in development mode (core or plugin): There's an f:password backed by a non-Secret */
if (req != null && (Boolean.getBoolean("hudson.hpi.run") || Boolean.getBoolean("hudson.Main.development"))) {
LOGGER.log(Level.WARNING, () -> "<f:password/> form control in " + getJellyViewsInformationForCurrentRequest() +
" is not backed by hudson.util.Secret. Learn more: https://jenkins.io/redirect/hudson.util.Secret");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this redirect be pointing here https://www.jenkins.io/doc/developer/security/secrets/ instead of the wiki?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, the redirect target is terrible, needs to be changed. But not worth doing another redirect for it. We probably need a new docs page to talk about this + toString.

Copy link
Member

@timja timja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested:

  1. adding a proxy with password in this PR, all worked fine, and no log messages
  2. hacking proxyconfiguration to be backed by a string, and strings getter/setter, got expected warning message with view:
Apr 30, 2020 8:55:28 PM hudson.Functions getPasswordValue
WARNING: <f:password/> form control in PluginManager/advanced.jelly is not backed by hudson.util.Secret. Learn more: https://jenkins.io/redirect/hudson.util.Secret
  1. string field, with secret getters and setters - no log message and field stored in plain text (Not handled but probably unlikely to happen)

LGTM

@daniel-beck daniel-beck added the on-hold This pull request depends on another event/release, and it cannot be merged right now label Apr 30, 2020
@daniel-beck
Copy link
Member Author

On-holding until after 2.235, just in case.

@jglick
Copy link
Member

jglick commented May 1, 2020

Rather than the on-hold label you could convert to a draft, which would temporarily block this from being merged.

@daniel-beck
Copy link
Member Author

Not in progress though, might send the wrong signal to reviewers.

@timja timja removed the on-hold This pull request depends on another event/release, and it cannot be merged right now label May 4, 2020
@timja
Copy link
Member

timja commented May 4, 2020

Removed on-hold as 2.235 released

@timja timja requested a review from a team May 4, 2020 19:47
@timja timja added the ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback label May 8, 2020
@timja
Copy link
Member

timja commented May 8, 2020

This PR is now ready for merge, let's merge it tomorrow if there's no negative feedback

Copy link
Member

@oleg-nenashev oleg-nenashev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deployed the patch on my test instance, and it looks to be OK.
I also do not see anything wrong in the code. Will proceed with merge

@oleg-nenashev oleg-nenashev merged commit 8a9a1b2 into jenkinsci:master May 10, 2020
@daniel-beck
Copy link
Member Author

Caused JENKINS-62305.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback rfe For changelog: Minor enhancement. use `major-rfe` for changes to be highlighted
Projects
None yet
6 participants